'use strict';

stareal
    .controller("ActiveDuiController", function ($scope,$interval,$document, $stateParams, $api, $sce, base64, $state, $alert,localStorageService,$timeout) {
        // var height=(document.body.clientHeight-parseInt(document.documentElement.style.fontSize)*1.3)+'px';
        function GetRequest(){
            var url = location.search; //获取url中"?"符后的字串
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                var strs = str.split("&");
                console.log(strs);
                for (var i = 0;i<strs.length; i++) {
                    if(strs[i].indexOf("?") != -1){
                        theRequest[strs[i].split("=")[0]]=strs[i].split("=")[1]+'='+strs[i].split("=")[2];
                        console.log(strs[i].split("=")[1]);
                        console.log(strs[i].split("=")[2]);
                    }else{
                        theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
                    }

                }
            }
            return theRequest;
        }
        var Request = new Object();
        Request = GetRequest();
        console.log( Request );
        if($stateParams.app==1){
            $scope.BackStatus=true;
            //$("#loadingGif").css("display",'none');
            $scope.dowload_appStatus=true;
            localStorageService.set('activeDuiUrl',Request.activeDuiUrl);
            localStorageService.set('activeDuiThumb',Request.activeDuiThumb);
            localStorageService.set('activeDuiTitle',Request.activeDuiTitle);
        }
        if($stateParams.id==1){
            $scope.venus360Id=localStorageService.get('activeDuiUrl')+'&isApp=1';
            $scope.shareUD=localStorageService.get('activeDuiUrl');
            $scope.thumb=localStorageService.get('activeDuiThumb');
            $scope.title=localStorageService.get('activeDuiTitle');
        }else{
            $scope.venus360Id=$stateParams.id+'&isApp=1';
            $scope.shareUD=$stateParams.id;
            $scope.thumb=$stateParams.thumb;
            $scope.title=$stateParams.title;
            localStorageService.set('activeDuiUrl', $scope.shareUD);
            localStorageService.set('activeDuiThumb', $scope.thumb);
            localStorageService.set('activeDuiTitle', $scope.title);
        }


        var height=$(window).height()+'px';
        var wid=$(window).width()+'px';
        $api.get("app/duiba/shareH5",{dbredirect:$scope.venus360Id},true)
            .then(function (ret) {
                var data=ret.data;
               // console.log(data);
                var link=data.url;
              //  console.log(link);
                // window.location.href=link;
                $("#venusGaocontainer").html('<iframe src="'+link+'" frameborder="0"  id="venusGaoconIframe" width="'+wid+'" height="'+height+'" ></iframe>');
                //window.open('https://activity.m.duiba.com.cn/chome/index?from=login&spm=53694.1.1.1','_blank');
                $scope.getShare();
                // console.log($scope.thumb);
                // console.log($scope.title);
            })
        //    接受https://m.amap.com/navi/?dest=119.284567%2C26.019484&destName=%E7%A6%8F%E5%B7%9E%E6%B5%B7%E5%B3%A1%E5%A5%A5%E6%9E%97%E5%8C%B9%E5%85%8B%E4%BD%93%E8%82%B2%E4%B8%AD%E5%BF%83%E7%BB%BC%E5%90%88%E9%A6%86&key=59ca8a781dba9f928faceafddbb2bf52
        if (!localStorageService.get('token')) {
                $scope.activeDuiDailogStatus=true;
        }else{
            $scope.activeDuiDailogStatus=false;
        }

        //分享
        //微信分享http://192.168.1.4:9090/oauth/getSignature
       // $scope.sharUrl='http://www.mydeershow.com/?&#/'; // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致'  正式
        $scope.sharUrl='https://test.djx.m.opentickets.cn/?&activeDuiUrl='+$scope.shareUD+'&activeDuiThumb='+$scope.thumb+'&activeDuiTitle='+$scope.title+'#/'; // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致' 测试
       // $scope.sharUrl='http://test.djx.m.opentickets.cn/?&#/'; // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致'
        $scope.getShare=function(){
            $api.get("app/share/getSignature", {url: window.location.href.split('#')[0]})
                .then(function (ret) {
                    if (ret) {
                        var data = ret.data;
                        wx.config({
                            debug: false,
                            appId: data.appid,
                            timestamp: data.timestamp,
                            nonceStr: data.nonceStr,
                            signature: data.signature,
                            jsApiList: [
                                'onMenuShareTimeline',
                                'onMenuShareAppMessage',
                                'onMenuShareQQ',
                                // 'onMenuShareWeibo',
                                'onMenuShareQZone'
                            ]
                        });
                        wx.ready(function () {
                            //分享到朋友圈
                            wx.onMenuShareTimeline({
                                title: $scope.title, // 分享标题
                                desc: '我在独角秀分享了一个不错的活动！', // 分享描述
                                link: $scope.sharUrl+'main/activeDui/1/1/1/1', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                                imgUrl: $scope.thumb, // 分享图标
                                success: function () {
                                    // 用户确认分享后执行的回调函数
                                    //alert('你好');
                                    $alert.show("分享成功");
                                },
                                cancel: function () {
                                    // 用户取消分享后执行的回调函数
                                    // alert('你好....');
                                }
                            });
                            //分享给朋友
                            wx.onMenuShareAppMessage({
                                title: $scope.title, // 分享标题
                                desc: '我在独角秀分享了一个不错的活动！', // 分享描述
                                link: $scope.sharUrl+'main/activeDui/1/1/1/1', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                                imgUrl: $scope.thumb, // 分享图标
                                type: 'link', // 分享类型,music、video或link，不填默认为link
                                dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
                                success: function () {
// 用户确认分享后执行的回调函数
                                    $alert.show("分享成功");
                                    // alert('你好....');
                                },
                                cancel: function () {
// 用户取消分享后执行的回调函数
                                }
                            });
                            wx.onMenuShareQQ({
                                title: $scope.title, // 分享标题
                                desc: '我在独角秀分享了一个不错的活动！', // 分享描述
                                link: $scope.sharUrl+'main/activeDui/1/1/1/1', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                                imgUrl: $scope.thumb, // 分享图标
                                success: function () {
// 用户确认分享后执行的回调函数
                                    $alert.show("分享成功");
                                },
                                cancel: function () {
// 用户取消分享后执行的回调函数
                                }
                            });
                            wx.onMenuShareQZone({
                                title: $scope.title, // 分享标题
                                desc: '我在独角秀分享了一个不错的活动！', // 分享描述
                                link: $scope.sharUrl+'main/activeDui/1/1/1/1', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                                imgUrl: $scope.thumb, // 分享图标
                                success: function () {
// 用户确认分享后执行的回调函数
                                    $alert.show("分享成功");
                                },
                                cancel: function () {
// 用户取消分享后执行的回调函数
                                }
                            });
                        });
                        wx.error(function (res) {
                            //console.log(res);
                            //alert("微信分享接口配置失败");
                        });
                    }
                })
        }
        $scope.goTo=function(){
            var  rs = "main.activeDui-" + JSON.stringify({id: 1,thumb:1,title:1});
            var token=localStorageService.get('token');
            var isbind = localStorageService.get('isbind');
            var openID = localStorageService.get('openid');
            var ua = window.navigator.userAgent.toLowerCase();
            if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                if (isbind != 1 && openID != '') {
                    localStorageService.set('cleartoken', token);
                    location.href = "#/main/register/" + encodeURIComponent(rs);
                    localStorageService.set('token', '');
                    return false
                }
            }
            if (!localStorageService.get('token')) {
                var  rs = "main.activeDui-" + JSON.stringify({id: 1,thumb:1,title:1});
                var ua = window.navigator.userAgent.toLowerCase();
                if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                    location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                        "appid=wxda73ac8ac7af1261&" +
                        "redirect_uri=http%3A%2F%2Ftest.djx.m.opentickets.cn%2Foauth%2Findex" +
                        "&response_type=code&scope=snsapi_userinfo&state="+encodeURIComponent(rs)+"&connect_redirect=1#wechat_redirect";
                } else {
                  console.log(encodeURIComponent(rs));
                    location.href = "#/main/login/"+encodeURIComponent(rs);
                }
                return false;
            }
        }

    })